package com.woniuxy.house.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.entity.ResponseResult;
import com.woniuxy.entity.house.Newhouse;
import com.woniuxy.entity.house.dto.HouseTypeInfoDto;
import com.woniuxy.entity.house.dto.SelectHouseDto;
import com.woniuxy.house.service.NewhouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liangyu
 * @since 2021-03-19
 */
@Api(tags = "新房接口",description = "管理新房的接口")
@RestController
@RequestMapping("/house/newhouse")
public class NewhouseController {

    @Resource
    private NewhouseService newhouseService;

    /**
     * 根据id查询新房,楼盘，户型等信息
     * @param id 新房id
     * @return
     */
    @ApiOperation("根据id查询新房,楼盘，户型等信息")
    @GetMapping("/findById")
    public ResponseResult<Newhouse> findNewHouseById(int id){
        Newhouse newhouse = newhouseService.findNewHouseById(id);
        if(newhouse!=null){
            return new ResponseResult<>(newhouse);
        }else{
            return new ResponseResult<>(500,"FAIL");
        }
    }

    /**
     * 查询某楼盘对应新房
     * @param id   楼盘id
     * @return
     */
    @ApiOperation("查询某楼盘对应新房")
    @GetMapping("/findByBuildingId")
    public ResponseResult<List<Newhouse>> findNewHouseByBuildingId(int id){
        List<Newhouse> newhouses = newhouseService.findNewHousesByBuildingId(id);
        if(newhouses.size()>0){
            return new ResponseResult<>(newhouses);
        }else{
            return new ResponseResult<>(500,"FAIL");
        }
    }

    /**
     * 楼盘新房上架
     * @param newhouse  新房上架
     * @return
     */
    @ApiOperation("楼盘新房上架")
    @PostMapping("/add")
    public ResponseResult insertHouseToBuilding(@RequestBody Newhouse newhouse){
        //添加新房
        //添加楼盘在售房屋数量
        if(newhouseService.insertNewhouseToBuilding(newhouse)){
            return ResponseResult.SUCCESS;
        }else{
            return ResponseResult.FAIL;
        }

    }

    /**
     * 新房出售下架
     * @param id  新房id
     * @return
     */
    @ApiOperation("新房出售下架")
    @PostMapping("/delete")
    public ResponseResult deleteNewHouse(int id){
        //删除房屋信息
        //修改楼盘在售数量和均价
        if(newhouseService.deleteNewHouse(id)){
            return ResponseResult.SUCCESS;
        }else{
            return ResponseResult.FAIL;
        }
    }

    /**
     * 新房信息修改
     * @param newhouse 新房信息
     * @return
     */
    @ApiOperation("新房信息修改")
    @PostMapping("/update")
    public ResponseResult updateNewHouseInfo(Newhouse newhouse){
        if(newhouseService.updateInfo(newhouse)){
            return ResponseResult.SUCCESS;
        }else{
            return ResponseResult.FAIL;
        }
    }

    /**
     * 分页查询所有新房
     * @param current
     * @param size
     * @return
     */
    @ApiOperation("分页查询所有新房")
    @GetMapping("/findPage")
    public ResponseResult<Page<Newhouse>> findPage(int current, int size){
        Page<Newhouse> newhousePage = newhouseService.page(new Page<>(current,size));
        return new ResponseResult<>(newhousePage);
    }

    /**
     * 条件分页查询新房
     * @param current
     * @param size
     * @param selectHouseDto
     * @return
     */
    @ApiOperation("条件分页查询新房")
    @GetMapping("/findByCondictions")
    public ResponseResult<List<HouseTypeInfoDto>> findByCondictions(int current, int size, SelectHouseDto selectHouseDto){
        List<HouseTypeInfoDto> houseTypeInfoDtos = newhouseService.findByCondictions(current, size, selectHouseDto);
        if(houseTypeInfoDtos.size()>0){
            return new ResponseResult<>(houseTypeInfoDtos);
        }else{
            return new ResponseResult<>(500,"fail");
        }
    }
}